import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.util.Calendar;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.XMLOutputter;

import com.sun.star.beans.Property;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.XPropertySetInfo;
import com.sun.star.document.XDocumentInfo;
import com.sun.star.document.XDocumentInfoSupplier;
import com.sun.star.frame.XController;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XModel;
import com.sun.star.lang.XComponent;
import com.sun.star.text.XPageCursor;
import com.sun.star.text.XText;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextViewCursor;
import com.sun.star.text.XTextViewCursorSupplier;
import com.sun.star.uno.UnoRuntime;



public class DashboardPlugin implements Serializable {
    public static void main(String[] args) {
        try {
            // get the remote office component context
            com.sun.star.uno.XComponentContext xContext =
                com.sun.star.comp.helper.Bootstrap.bootstrap();
            System.out.println("Connected to a running office ...");
            com.sun.star.lang.XMultiComponentFactory xMCF =
                xContext.getServiceManager();
            String available = (xMCF != null ? "available" : "not available");
            Object desktop=  xMCF.createInstanceWithContext( "com.sun.star.frame.Desktop" , xContext);
            XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(
            	     XDesktop.class, desktop);
            	XComponent xCurrentComponent = xDesktop.getCurrentComponent();
            	XModel xModel = (com.sun.star.frame.XModel)UnoRuntime.queryInterface(XModel.class, xCurrentComponent);
            	XController xController =  xModel.getCurrentController();
            	XTextViewCursorSupplier xViewCursorSupplier =
            	    (XTextViewCursorSupplier)UnoRuntime.queryInterface(
            	        XTextViewCursorSupplier.class, xController);
            	XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();

            	XPropertySet xCursorPropertySet = (XPropertySet)UnoRuntime.queryInterface(
            	    XPropertySet.class, xViewCursor);

            	xCursorPropertySet.setPropertyValue("CharStyleName", "Quotation");
            	xCursorPropertySet.setPropertyValue("ParaStyleName", "Quotations");

            	XPageCursor xPageCursor = (XPageCursor)UnoRuntime.queryInterface(
            	    XPageCursor.class, xViewCursor);
            	System.out.println("The current page number is " + xPageCursor.getPage());

            	XText xDocumentText = xViewCursor.getText();
            	XTextDocument xtd = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class,xCurrentComponent);
            	XDocumentInfoSupplier xdis = (XDocumentInfoSupplier)UnoRuntime.queryInterface(XDocumentInfoSupplier.class,xtd);
                XDocumentInfo xdi = xdis.getDocumentInfo();
                XPropertySet xps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xdi);
                XPropertySetInfo xpsi = xps.getPropertySetInfo();
                Property[] props = xpsi.getProperties();
                for(Property p: props){
                	System.out.println(p.Name + " : " + xps.getPropertyValue(p.Name));
                }
               
                
            System.out.println( "remote ServiceManager is " + available );

            writeClue( xDocumentText.getString(),props, xps);
        }
        catch (java.lang.Exception e){
            e.printStackTrace();
        }
        finally {
            System.exit(0);
        }
      }
    public static void writeClue (String s, Property [] propset, XPropertySet xps){
//    	String path = System.getenv("HOME");
//    	path += "/.dashboard/Messages/";
//    	System.out.println(path);
    	
    	
    	String Guid = new RandomGUID().toString();
    	Guid = Guid.toLowerCase();
		//String f = new String(path + Guid +  ".xml" );
    	
    	Element root = new Element("Message");
		XMLOutputter serializer = new XMLOutputter();
		Namespace xsd =
			  Namespace.getNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
		Namespace xsi =
		  Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
		root.addNamespaceDeclaration(xsd);
		root.addNamespaceDeclaration(xsi);
		
		root.setAttribute("type", "ClueMessage", xsi);
		//root.setAttribute("type", "ClueMessage");
		root.setAttribute("Id", Guid );
		//Timestamp="2006-10-16T18:19:40.6069560-04:00"
		root.setAttribute("Timestamp",String.format("%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS.%1$tm%1$tz", (Calendar.getInstance(java.util.TimeZone.getTimeZone("EST")))));
		root.setAttribute("Path", "/event/document/open");
		root.setAttribute("Window", "0x5000061"); //FIXME! Hacky!
		Document doc = new Document (root);
		Element clues = new Element("Clues");
		root.addContent(clues);
		Element clue = new Element("Clue");
		clue.setAttribute("Type", "db:document");
		clues.addContent(clue);
		Element props = new Element("Properties");
		clue.addContent(props);
		Element prop = new Element("Property");
		prop.setAttribute("Name", "dc:content");
		prop.setAttribute("Value",s);
		//prop.addContent(s);
		prop.setAttribute("Type", "dc:text");
		props.addContent(prop);
		
		
		
		try {
			Socket sock = new Socket("127.0.0.1",5369);  
			OutputStream outStr = sock.getOutputStream();
			serializer.output(doc, System.out); 
		    serializer.output(doc, outStr);
		    outStr.write(0xff);
		    outStr.flush();
		    outStr.close();
		    }
		    catch (IOException e) {
		      System.err.println(e);
		    }
		
    }
}
    
